﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    /// <summary>
    /// 数据库连接字串生成器
    /// </summary>
    public interface IConnectStringBuilder
    {
         string ConnectString { get;  }
        //TODO:其他常见数据库的连接字串生成，待补充
    }
    /// <summary>
    /// MySql的连接字串生成器
    /// </summary>
    public class MySqlConnectStringBuider : IConnectStringBuilder
    {
        public string Source { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
        public string Database { get; set; }
        public string ConnectString { get {
            return $"Host={Source};UserName={User};Password={Password};Database={Database};";
            } }
        public MySqlConnectStringBuider() { }
        public MySqlConnectStringBuider(string connString)
        {//"Host=server;UserName=root;Password=password;Database=SoftwareManager;"
            string[] strs = connString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var str in strs)
            {
                if (Regex.IsMatch(str, "^Host=", RegexOptions.IgnoreCase))
                {
                    Source = str.Substring(5);
                    continue;
                }
                if (Regex.IsMatch(str, "^UserName=", RegexOptions.IgnoreCase))
                {
                    User = str.Substring(9);
                    continue;
                }
                if (Regex.IsMatch(str, "^Password=", RegexOptions.IgnoreCase))
                {
                    Password = str.Substring(9);
                    continue;
                }
                if (Regex.IsMatch(str, "^Database=", RegexOptions.IgnoreCase))
                {
                    Database = str.Substring(9);
                    continue;
                }
            }
            
        }
    }
    public class SqLiteConnectStringBuider : IConnectStringBuilder
    {
        public string ConnectString { get
            {
                string connStr = $"Data Source={DbFile};Version=3;";
                if (!string.IsNullOrEmpty(Password)) connStr += "Password={Password};";
                return connStr;
            }
        }
        public string DbFile { get; set; }
        public string Password { get; set; }
        public float Version { get; set; }
        public SqLiteConnectStringBuider()
        {
            Version = 3;
        }
        public SqLiteConnectStringBuider(string connString)
        {//"Data Source=datafile;Version=3;Password=password;"
            string[] strs = connString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var str in strs)
            {
                if (Regex.IsMatch(str, "^Data Source=", RegexOptions.IgnoreCase))
                {
                    DbFile = str.Substring(12);
                    continue;
                }
                if (Regex.IsMatch(str, "^Version=", RegexOptions.IgnoreCase))
                {
                    Version = float.Parse(str.Substring(8));
                    continue;
                }
                if (Regex.IsMatch(str, "^Password=", RegexOptions.IgnoreCase))
                {
                    Password = str.Substring(9);
                    continue;
                }
            }
            
        }
    }
}
